package com.yn.pet.controller.app;


import com.yn.common.core.domain.AjaxResult;
import com.yn.pet.controller.app.biz.ASettingsBizService;
import com.yn.pet.controller.pc.biz.DeviceInfoBizService;
import com.yn.pet.controller.pc.response.MileageCountResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Date;

@RestController
@AllArgsConstructor
@RequestMapping("/front/frequent/services")
@Api(tags = "app端——常用服务")
@Slf4j
public class AFrequentServicesController {

    private final ASettingsBizService settingsBizService;

    private final DeviceInfoBizService deviceInfoBizService;


    /**
     * 获取定位模式信息
     * @param deviceId 设备ID
     * @return
     */
    @GetMapping("locationModel/info")
    @ApiOperation("获取定位模式信息")
    public AjaxResult locationModelInfo(@RequestParam @NotNull Long deviceId) {
        log.info("获取定位模式信息入参,deviceId:{}", deviceId);
        return AjaxResult.success(settingsBizService.getLocationModelInfo(deviceId));
    }

    /**
     * 修改定位模式状态
     * @param deviceId 设备id
     * @param type 定位模式类型
     * @param minutes  分钟
     * @return
     */
    @PostMapping("locationModel/update")
    @ApiOperation("修改定位模式")
    public AjaxResult updateLocationModel(@RequestParam @NotNull Long deviceId,
                                          @RequestParam @NotNull(message = "定位模式类型不能为空") Integer type,
                                          @RequestParam(required = false,name = "minutes") String minutes) {
        log.info("修改定位模式入参,deviceId:{}  type:{},minutes:{}", deviceId,type, minutes);
        settingsBizService.updateLocationModel(deviceId,type, minutes);
        return AjaxResult.success("修改成功");
    }

    /**
     * 获取设备里程统计
     * @param deviceId 设备id
     * @param startTime 开始时间
     * @param endTime 结束区间
     * @return MileageCountResponse
     */
    @GetMapping("mileage/count")
    @ApiOperation("设备里程统计")
    public AjaxResult mileageCount(@RequestParam @NotNull Long deviceId,
                                   @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                   @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        log.info("设备里程统计入参,deviceId:{},startTime:{},beginTime:{}", deviceId,startTime,endTime);
        MileageCountResponse response = settingsBizService.getMileageCount(deviceId,startTime,endTime);
        return AjaxResult.success(response);
    }

    /**
     * 更新宠物类型
     * @param deviceId  设备id
     * @param petType 宠物类型
     * @return
     */
    @GetMapping("updatePetType")
    @ApiOperation("设备功能页面更新宠物类型")
    public AjaxResult updatePetType(@RequestParam @NotNull Long deviceId,
                                    @RequestParam @NotNull Integer petType) {
        deviceInfoBizService.updatePetType(deviceId, petType);
        return AjaxResult.success("更新成功");
    }

    @PostMapping("setHeartbeatTime")
    @ApiOperation("设置设备心跳时间")
    public AjaxResult setHeartbeatTime(@RequestParam @ApiParam("时间间隔") Integer time,@RequestParam @ApiParam("imei") String imei) {
        settingsBizService.setHeartbeatTime(time,imei);
        return AjaxResult.success("设置成功");
    }
}
